package tp.badugi.bot.guiViewsBot;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;

import tp.badugi.bot.GuiClientWithBot;

public class WinnerViewBot extends JFrame{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 5652419216228763054L;

	private GuiClientWithBot client;
	
	private final Dimension winnerViewSize;
	
	private final int winnerViewTimeout = 15;
	
	public Timer winnerTimer;
	
	public JButton winnerCloseViewButton = new JButton("Close window");
	
	public WinnerViewBot(GuiClientWithBot client, String[][] revealedPlayersCards,
			String[] playerNames, long[] chipsAddedFromPot,
			int[] winnersIndexes) {
		
		this.client = client;
		
		this.winnerTimer = new Timer(this.winnerViewTimeout*1000, this.client);
		
		this.winnerViewSize = new Dimension(400, 50*playerNames.length + 50);
		
		this.makeView();
		this.createNewView(revealedPlayersCards, playerNames, chipsAddedFromPot, winnersIndexes);
	}

	private void makeView(){
		
		this.setBounds(	(int)(this.client.windowBoundPoint.getX() + this.winnerViewSize.getWidth()/2),
									(int)(this.client.windowBoundPoint.getY()),
									(int)this.winnerViewSize.getWidth(),
									(int)this.winnerViewSize.getHeight());
		
		this.setFont(this.client.clientFont);
		
		this.setLayout(new BoxLayout(this.getContentPane(),BoxLayout.Y_AXIS));
		
		this.winnerCloseViewButton.addActionListener(this.client);
		this.winnerCloseViewButton.setBorder(new EmptyBorder(10, 110, 10, 110));
		
		this.setUndecorated(true);
		this.setResizable(false);
	}
	
	public void createNewView(String[][] revealedPlayersCards, String[] playerNames,
			long[] chipsAddedFromPot, int[] winnersIndexes) {
		
		this.getContentPane().removeAll();
		this.getContentPane().repaint();
		
		int numberOfPlayers = revealedPlayersCards.length;
		int numberOfWinners = winnersIndexes.length;	
		
		WinnerLabelBot label;
		WinnerCardButtonBot cardButton;
		WinnerPlayerPanelBot playerPanel;
		
		Image image;
		Image scaledImage;
		ImageIcon scaledImageIcon;
		
		for(int i = 0; i < numberOfPlayers; i++){
			
			playerPanel = new WinnerPlayerPanelBot((int)this.winnerViewSize.getWidth(),50);
			
			this.add(playerPanel);
			
			 // reseting background
			playerPanel.setBackground(Color.GREEN);
			
			for(int j = 0; j < numberOfWinners; j++){
				if( i == winnersIndexes[j]){
					playerPanel.setBackground(Color.YELLOW);
				}
			}
			
			label = new WinnerLabelBot(100,20);
			label.setText(playerNames[i]);
			
			playerPanel.add(label);
			
			playerPanel.add(Box.createHorizontalGlue());
			
			for(int j = 0; j < 4; j++){
				cardButton = new WinnerCardButtonBot(50,50);
				image = this.client.imageProvider.getCardImage(revealedPlayersCards[i][j]);
				scaledImage = image.getScaledInstance(50,50, Image.SCALE_SMOOTH);
				scaledImageIcon = new ImageIcon(scaledImage);
				cardButton.setIcon(scaledImageIcon);
				cardButton.setDisabledIcon(scaledImageIcon);
				playerPanel.add(cardButton);
			}
			
			playerPanel.add(Box.createHorizontalGlue());
			
			label = new WinnerLabelBot(100,50);
			
			label.setText(String.valueOf(chipsAddedFromPot[i]));
			
			playerPanel.add(label);
			
			this.add(playerPanel);
		}
		
		this.add(this.winnerCloseViewButton);
		
		this.winnerTimer.start();
		
		this.setVisible(true);
	}
}
